package com.dn.luckbi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 全局跨域配置
 *
 * @author <a href="https://github.com/lidn">程序员dn</a>
 * @from <a href="https://dn.icu">yyds</a>
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        // 允许访问的域名
        String[] origins = {
                "http://localhost:8000",
                "http://luckbi.rjxnx.fun:10001",
                "http://luckbi.rjxnx.fun:40001",
                "http://luckbi.rjxnx.fun:10001",
                "http://luckbi.rjxnx.fun:40001",
        };
        // 覆盖所有请求
        registry.addMapping("/**")
                // 允许发送 Cookie
                .allowCredentials(true)
                // 放行哪些域名（必须用 patterns，否则 * 会和 allowCredentials 冲突）
                .allowedOriginPatterns("*")
//                .allowedOriginPatterns(origins)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

}
